﻿using System.Data.SqlClient;
using System.Threading.Tasks;
using H2.NET.Data.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using H2.NET.Web.Pages.SMD.Model;

namespace H2.NET.Web.Pages.SMD.Helper
{
    public static class AutoTaskHelper
    {

        /*
        #region 新开门店相关的自动发起流程

        /// <summary>
        /// 新开门店 -> 保证金收取
        /// </summary>
        public static void Deposit_Receive(this TaskAPI parentTaskApi,
            t_StoreMasterData store, int dealerId, int deviceid)

        {
            var user = UserHelper.GetUserAPI();

            var taskApi = new TaskAPI(TaskHelper.STORE_DEPOSIT_RECEIVE_TASKID);
            var applicationNo =
                Util.GenerateSequence(((object[]) taskApi["PrefixNo"])[0] + "-" + DateTime.Now.ToString("yyMM"), "", 4);
            taskApi["IsRenovation"] = "1";
            taskApi["BlankVlue"] = parentTaskApi.GetVariableBySingleValue("BlankVlue");
            taskApi["ParentProcess"] = parentTaskApi.Process;
            taskApi["ParentIncident"] = parentTaskApi.Incident;
            taskApi["AutoBusinessKey"] = Constant.PROCESS_STORE;
            taskApi.InitTask(user, applicationNo);
            if (!string.IsNullOrEmpty(store.ApplicationType))
            {
                taskApi["DeviceId"] = deviceid;
                taskApi["IsDevice"] = "1";
                taskApi.Summary = "汇款底单上传 / " + TaskHelper.getSummary(parentTaskApi.Summary);
                taskApi["Deposit"] = store.MachineDeposit.Value.ToString();
            }
            else
            {
                taskApi["IsDevice"] = "0";
                taskApi.Summary = "履约保证金确认 / " + TaskHelper.getSummary(parentTaskApi.Summary);
            }

            var channel = store.Channel == "多乐士" ? "DULUX" : "LEVIS";
            taskApi["DealerID"] = dealerId;
            taskApi["AuthID"] = store.AuthID;
            taskApi["StoreType"] = store.StoreType;
            taskApi["Channel"] = channel;

            taskApi.SetDepositReceiveRecipient(store.AreaName.ToUpper(), store.ProvinceName,
                channel.ToUpper());

            if (!taskApi.Send()) return;
            var ass = taskApi.InitAssessment(user);
            Storage.Save(Storage.Context1, ass);
        }

        #endregion

        #region 经销商变更相关的自动发起流程

        /// <summary>
        /// 经销商变更 -> 合同主数据变更
        /// </summary>
        public static void ChangeContract(this TaskAPI parentTaskApi, int storeid, string region,
            string province)
        {

            var dictionary = new Dictionary<string, object>
            {
                {"AutoBusinessKey", Constant.PROCESS_CHANGEDEALER},
                {"BlankVlue", storeid},
                {"Actions", "ChangeContract"}
            };

            DataChange(parentTaskApi, region, province,
                "合同信息完善 / 经销商- " + TaskHelper.getSummary(parentTaskApi.Summary),
                "Store", dictionary);
        }

        /// <summary>
        /// 经销商变更 -> 门店基本信息变更
        /// </summary>
        public static void ChangeStore(this TaskAPI parentTaskApi, int storeid, string region,
            string province)
        {
            var dictionary = new Dictionary<string, object>
            {
                {"AutoBusinessKey", Constant.PROCESS_CHANGEDEALER},
                {"BlankVlue", storeid},
                {"Actions", "ChangeStore"}
            };

            DataChange(parentTaskApi, region, province,
                "门店基本信息变更 / 经销商- " + TaskHelper.getSummary(parentTaskApi.Summary), 
                "Store", dictionary);
        }

        /// <summary>
        /// 经销商变更 -> 保证金变更
        /// </summary>
        /// <param name="parentTaskApi"></param>
        public static void DepositChange(this TaskAPI parentTaskApi, int storeid, string region,
            string province)
        {
            var user = UserHelper.GetUserAPI();
            var taskApi = new TaskAPI(TaskHelper.STORE_DEPOSIT_CHANGE_TASKID);
            var applicationNo =
                Util.GenerateSequence(((object[])taskApi["PrefixNo"])[0] + "-" + DateTime.Now.ToString("yyMM"), "", 4);

            taskApi["ParentProcess"] = parentTaskApi.Process;
            taskApi["ParentIncident"] = parentTaskApi.Incident;
            taskApi["AutoBusinessKey"] = Constant.PROCESS_CHANGEDEALER;
            taskApi["BlankVlue"] = storeid;
            taskApi["Actions"] = "ChangeStore";
            taskApi.InitTask(user, applicationNo);
            taskApi.Summary = "门店基本信息变更 / 经销商- " + TaskHelper.getSummary(parentTaskApi.Summary);
            
            taskApi.SetDepositChangeRecipient(region, province);

            if (!taskApi.Send()) return;
            var ass = taskApi.InitAssessment(user);
            Storage.Save(Storage.Context1, ass);
        }

        #endregion

        /// <summary>
        /// 发起主数据变更
        /// </summary>
        /// <param name="taskapi"></param>
        /// <param name="storeid"></param>
        /// <param name="region"></param>
        /// <param name="province"></param>
        private static void DataChange(TaskAPI parentTaskApi, string region, string province,
            string summary, string category, Dictionary<string, object> dictionary)
        {
            var user = UserHelper.GetUserAPI();
            var taskApi = new TaskAPI(TaskHelper.STORE_DATA_CHANGE_TASKID);
            var applicationNo =
                Util.GenerateSequence(((object[])taskApi["PrefixNo"])[0] + "-" + DateTime.Now.ToString("yyMM"), "", 4);
            
            taskApi["ParentProcess"] = parentTaskApi.Process;
            taskApi["ParentIncident"] = parentTaskApi.Incident;
            foreach (var keyvalue in dictionary)
            {
                taskApi[keyvalue.Key] = keyvalue.Value;
            }
            taskApi.InitTask(user, applicationNo);
            taskApi.Summary = summary;

            taskApi.SetStoreDataChangeRecipient(region.ToUpper(), province, category);

            if (!taskApi.Send()) return;
            var ass = taskApi.InitAssessment(user);
            Storage.Save(Storage.Context1, ass);
        }

        */
    }
}